package cc.owoo.godpen.network.throughput;

import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 接收流量
 * Created by nimensei
 * 2022-03-31 上午 01:58
 **/
public class WriteThroughput implements Throughput {
    private final AtomicLong size = new AtomicLong();// 流量

    /**
     * 创建并监听输出流
     *
     * @param output 输出流
     * @return 监听中的输出流
     */
    public OutputStream count(OutputStream output) {
        return new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                if (b != -1)
                    size.addAndGet(1);
                output.write(b);
            }
        };
    }

    @Override
    public long size() {
        return size.get();
    }
}
